﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    class DalThongKeBaoCao
    {
        internal static List<BizThongKe> GetListThongKe(DateTime fromDate, DateTime toDate)
        {

            List<BizThongKe> result = new List<BizThongKe>();

            QLDCTTEntities da = new QLDCTTEntities();

            try
            {
                result = (from ctk in da.ChiTietKho
                          where ctk.Ngay >= fromDate && ctk.Ngay <= toDate
                          group ctk by ctk.ID_SP into g
                          select new BizThongKe()
                          {
                              ID_SP = g.Key,
                              Ngay = g.Max(x => x.Ngay),
                              TenSP = g.FirstOrDefault(x => x.Ngay == g.Max(y => y.Ngay)).SanPham.TenSP,
                              TenLoai = g.FirstOrDefault(x => x.Ngay == g.Max(y => y.Ngay)).SanPham.LoaiSanPham.TenLoai,
                              SoLuongTon = g.FirstOrDefault(x => x.Ngay == g.Max(y => y.Ngay)).SoLuongTon
                          }).ToList();


            }

            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        internal static List<BizBaoCao> GetListBaoCao(DateTime fromDate, DateTime toDate)
        {

            List<BizBaoCao> result = new List<BizBaoCao>();
            List<BizBaoCao> result2 = new List<BizBaoCao>();


            QLDCTTEntities da = new QLDCTTEntities();

            try
            {
                result = (from dh in da.DonDatHang
                          from ct in dh.ChiTietDDH
                          where dh.Ngay >= fromDate && dh.Ngay <= toDate && dh.TrangThaiGiaoHang == true
                          select new BizBaoCao()
                          {
                            Ngay = dh.Ngay,
                            ID_SP = ct.ID_SP,
                            TenSP = ct.SanPham.TenSP,
                            SoLuongDaBan = ct.SoLuong,
                            ThanhTien = ct.SoLuong * ct.SanPham.DonGia
                          }).ToList();

                result2 = (from item in result
                           group item by item.ID_SP into g
                           select new BizBaoCao()
                           {
                               Ngay = g.FirstOrDefault(x => x.ID_SP == g.Key).Ngay,
                               ID_SP = g.Key,
                               TenSP = g.FirstOrDefault(x => x.ID_SP == g.Key).TenSP,
                               SoLuongDaBan = g.Where(x => x.ID_SP == g.Key).Sum(y => y.SoLuongDaBan),
                               ThanhTien = g.Where(x => x.ID_SP == g.Key).Sum(y => y.ThanhTien)
                           }).ToList();

             }

            catch (Exception ex)
            {
                throw ex;
            }

            return result2;
        }
    }
}
